using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Forms;
using MimeCloud.AssetManagement.AssetRendering.AssetContent;
using MimeCloud.AssetManagement.AssetRendering.Metadata;
using MimeCloud.AssetManagement.Business.Interface;
using MimeCloud.AssetManagement.Editor;
using Orientation=System.Windows.Forms.Orientation;

namespace MimeCloud.AssetManagement.AssetRendering
{
    /// <summary>
    /// Interaction logic for UserControl1.xaml
    /// </summary>
    public partial class DigitalAsset : System.Windows.Controls.UserControl
    {
        private IAsset _childAsset;

        public DigitalAsset()
        {
            InitializeComponent();
        }

        public IAsset ChildAsset
        {
            get
            {
                return _childAsset;
            }
            set
            {
                _childAsset = value;
                DisplayAsset(_childAsset);
            }
        }

        public void UpdateDisplay()
        {
            if (_childAsset!=null)
                DisplayAsset(_childAsset);
        }

        public void DisplayAsset(IAsset childAsset)
        {
            _childAsset = childAsset;

            //details
            PropertyGrid mdGrid = new PropertyGrid();
            mdGrid.SelectedObject = childAsset;
            mdWFHOST.Child = mdGrid;

            //content
            AssetContentTabs.Items.Clear();
            if (childAsset.AssetContentList!=null)
            {
                foreach (IAssetContent content in childAsset.AssetContentList)
                {
                    IAssetContentViewer contentView = AssetContentControlProvider.ProvideViewerControl(content);
                    contentView.DisplayAssetContent(content);
                    AssetContentTabs.Items.Add(new TabItem {Header = ItemHeader(content), Content=contentView});
                }
                if (AssetContentTabs.Items.Count > 0 && AssetContentTabs.SelectedIndex < 0)
                    AssetContentTabs.SelectedIndex = 0;
            }

            //md
            MetadataKeys.Children.Clear();
            foreach (var key in AppCommon.Current.AllMetadataKeys)
            {
                //make control
                MetadataGroupEdit mdDisplay = new MetadataGroupEdit();
                mdDisplay.MetaKey = key;
                List<IMetaData> contentValues =
                    (from md in childAsset.AssetMetaDataList where md.MetaKey.ID == key.ID select md).ToList();
                mdDisplay.ViewMetadata = contentValues;
                mdDisplay.masterList = childAsset.AssetMetaDataList;
                //add control
                MetadataKeys.Children.Add(mdDisplay);
            }
        }

        private StackPanel ItemHeader(IAssetContent content)
        {
            StackPanel result = new StackPanel();
            result.Orientation = System.Windows.Controls.Orientation.Horizontal;
            Image newimg = new Image();
            if (content.AssetContentType.ContentType == TypeOfContent.Image)
                newimg.Source = new BitmapImage(new Uri(@"pack://application:,,/Images/applicationicon.png"));
            else
            {
                newimg.Source = new BitmapImage(new Uri(@"pack://application:,,/Images/videoicon.png"));
            }
            newimg.Height = 18;
            newimg.Stretch = Stretch.Uniform;

            TextBlock txt = new TextBlock();
            txt.Text = content.AssetContentType.Name;
            txt.Margin = new Thickness(2,0,0,0);
            txt.VerticalAlignment = System.Windows.VerticalAlignment.Center;

            result.Children.Add(newimg);
            result.Children.Add(txt);

            return result;
        }
    }
}
